const query = require('../db');
const util = require('../util');
module.exports = {
       //添加题目
       addQuestion(data,callback,errback){
            let PK_QuestionGuid = util.createGuid();
            let sql =`INSERT INTO question (PK_QuestionGuid,FK_PaperGuid,\`Content\`,\`Option\`,\`Type\`,\`Status\`,\`Index\`) VALUES('${PK_QuestionGuid}','${data.FK_PaperGuid}','${data.Content}','${data.Option}','${data.Type}',1,${data.Index})`;
            query(sql,(err,data)=>{
                if(err){
                     util.writeErr(err+',sql='+sql+'时间='+new Date+',');
                     errback()
                }else{
                    callback(PK_QuestionGuid);
                }
            })
       },
       //根据问卷Guid获取题目（未删除）
       getQuestion(FK_PaperGuid,callback){
            let sql = `SELECT * FROM question WHERE FK_PaperGuid = '${FK_PaperGuid}' AND Status =1 ORDER BY \`Index\` ASC`;
            query(sql,(err,data)=>{
                if(err){
                    util.writeErr(err+',sql='+sql+'时间='+new Date+',');
                }else{
                    callback(data);
                }
            })
       },
       //根据题目Guid获取题目
       getSingleQuestion(PK_QuestionGuid,callback){
              let sql = `SELECT * FROM question WHERE PK_QuestionGuid = '${PK_QuestionGuid}'`;
              query(sql,(err,data)=>{
                  if(err){
                       util.writeErr(err+',sql='+sql+'时间='+new Date+',');
                  }else{
                      callback(data);
                  }
              })
       },
       //根据题目的Guid修改题目（修改题目内容，选项以及类型）
       updateQuestion(data,callback){
            let sql = `UPDATE question SET \`Content\`='${data.Content}',\`Option\`='${data.Option}',\`Type\`='${data.Type}' WHERE PK_QuestionGuid='${data.PK_QuestionGuid}'`
            query(sql,(err)=>{
                if(err){
                    util.writeErr(err+',sql='+sql+'时间='+new Date+',');
                }else{
                    this.getSingleQuestion(data.PK_QuestionGuid,callback);
                }
            })
       }
}